/**
 * @project spring-threadpool-async
 * @description
 * @errorcode SystemException</ br>
 * @author 2022 11:05 huzhengyang Create 1.0 <br>
 * @copyright ©2022-2023
 */

package com.teradata.async.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ConcurrentTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.annotation.Resource;
import java.util.concurrent.Executors;

@Configuration
@EnableAsync
public class AsyncConfig  implements AsyncConfigurer {

    @Resource(name = "asyncServiceExecutor")
    private ThreadPoolTaskExecutor asyncServiceExecutor;

    @Bean
    protected WebMvcConfigurer webMvcConfigurer() {
        return new WebMvcConfigurerAdapter() {
            @Override
            public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
                configurer.setTaskExecutor(getTaskExecutor());
            }
        };
    }

    @Bean
    protected ConcurrentTaskExecutor getTaskExecutor() {
        // return new ConcurrentTaskExecutor(Executors.newFixedThreadPool(5));
        return new ConcurrentTaskExecutor(asyncServiceExecutor);
    }
}